package Chapter06;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 使用线程池
 * Created by a on 2017/6/14.
 */
public class Test143 {
    public static void main(String[] args) {
        Runtime run = Runtime.getRuntime();
        run.gc();
        long freeMemory = run.freeMemory();
        long currentTime = System.currentTimeMillis();
        for (int i =0 ;i<1000;i++){
            new Thread(new TempThread()).start();
        }
        System.out.println("独立运行1000个线程所占用的内存:"+(freeMemory-run.freeMemory())+"字节");
        System.out.println("独立运行1000个线程所消耗的时间:"+(System.currentTimeMillis()-currentTime)+"毫秒");
        run.gc();
        freeMemory = run.freeMemory();
        currentTime = System.currentTimeMillis();
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        for (int i =0 ;i<1000;i++){
            executorService.submit(new TempThread());
        }
        System.out.println("使用线程池运行1000个线程所占用的内存:"+(freeMemory-run.freeMemory())+"字节");
        System.out.println("使用线程池1000个线程所消耗的时间:"+(System.currentTimeMillis()-currentTime)+"毫秒");
    }
}

class TempThread implements Runnable {
    private  int id = 0;

    @Override
    public void run() {
        id++;
    }




}

